/*
 * Created on 2007-9-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.tongji.quickCRUDQ.dao.jdbc;

import com.tongji.quickCRUDQ.webapp.QueryParam;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class OraclePagination implements Pagination {

	/* (non-Javadoc)
	 * @see com.tongji.quickCRUDQ.dao.jdbc.Pagination#formatPageSQL(com.tongji.quickCRUDQ.dao.jdbc.QueryParam)
	 */
	public String formatPageSQL(String sql, QueryParam queryParam) {
		StringBuffer pageSql=new StringBuffer();
		int max=queryParam.getRowsOfPage()*queryParam.getPageNo();
		int min=max-queryParam.getRowsOfPage()+1;
		pageSql.append("select * from (select tab.*,rownum rwnm from (\n ")
		       .append(sql).append(" \n) tab) where rwnm between ")
			   .append(min).append(" and ").append(max);
		return pageSql.toString();
	}

	/* (non-Javadoc)
	 * @see com.tongji.quickCRUDQ.dao.jdbc.Pagination#formatCountSQL(java.lang.String)
	 */
	public String formatCountSQL(String sql) {
		StringBuffer countSql=new StringBuffer();
		countSql.append("select count(*) cnt from (\n").append(sql).append("\n)");
		return countSql.toString();
	}

	/* (non-Javadoc)
	 * @see com.tongji.quickCRUDQ.dao.jdbc.Pagination#formatRownumSQL(java.lang.String, java.lang.String)
	 */
	public String formatRownumSQL(String sql, String id, CommonDao commonDao) {
		StringBuffer rownumSql=new StringBuffer();
		rownumSql.append("select rwnm from (select tab.*,rownum rwnm from (\n")
			     .append(sql).append("\n) tab)  where id='").append(id).append("'");
		return rownumSql.toString();
	}

}
